package com.panicstyle.Gongdong;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class ArticleView extends Activity {
	/** Called when the activity is first created. */
	protected String itemsTitle;
	protected String itemsLink;
	protected HttpClient httpClient;
	protected HttpContext httpContext;
	
	String g_isPNotice;
	String g_isNotice;
	String g_Subject;
	String g_UserName;
	String g_UserID;
	String g_Date;
	String g_Link;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        
        GongdongApplication app = (GongdongApplication)getApplication();
        httpClient = app.httpClient;
        httpContext = app.httpContext;
		
        intenter();

/*        
        // Login
		Login login = new Login();
		
		if (!login.LoginTo(httpClient, httpContext, cookieStore)) {
			return;
		}
*/
		
		String htmlDoc = getData(httpClient, httpContext);
		
		WebView webview = new WebView(this);
		setContentView(webview);
		webview.loadData(htmlDoc, "text/html; charset=UTF-8", null);
    }
    
    public void intenter() {
//    	Intent intent = getIntent();  // 값을 가져오는 인텐트 객체생성
    	Bundle extras = getIntent().getExtras();
    	// 가져온 값을 set해주는 부분
    	g_isPNotice = extras.getString("ISNOTICE").toString();
    	g_isNotice = extras.getString("ISNOTICE").toString();
    	g_Subject = extras.getString("SUBJECT").toString();
    	g_UserName = extras.getString("USERNAME").toString();
    	g_UserID = extras.getString("USERID").toString();
    	g_Date = extras.getString("DATE").toString();
    	g_Link = extras.getString("LINK").toString();
    }
    
    protected String getData(HttpClient httpClient, HttpContext httpContext) {		
		
		String url = "http://cafe.gongdong.or.kr" + g_Link;
		HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.requestGet(httpClient, httpContext, url);

        String strTitle = "<div align='left'>" + g_Subject + "</div><br /><div align='right'>" + g_UserName + " [" + g_Date + "]</div><hr>";
        
        Pattern p = Pattern.compile("(?<=<!---- contents start 본문 표시 부분 DJ ---->)(.|\\n)*?(?=<!---- contents end ---->)", Pattern.CASE_INSENSITIVE); 
        Matcher m = p.matcher(result);
        
        String strContent;
        if (m.find()) { // Find each match in turn; String can't do this.     
        	strContent = m.group(0);
        } else {
        	strContent = "";
        }

        p = Pattern.compile("(<p align=center><img onload=\"resizeImage2(this)\")(.|\\n)*?(</td>)", Pattern.CASE_INSENSITIVE); 
        m = p.matcher(result);
        
        String strImage;
        if (m.find()) { // Find each match in turn; String can't do this.     
        	strImage = m.group(0);
        } else {
        	strImage = "";
        }
        
        p = Pattern.compile("(?<=<!-- 댓글 시작 -->)(.|\\n)*?(?=<!-- 댓글 끝 -->)", Pattern.CASE_INSENSITIVE); 
        m = p.matcher(result);
        
        String strComment;
        if (m.find()) { // Find each match in turn; String can't do this.     
        	strComment = m.group(0);
        } else {
        	strComment = "";
        }
        
        strComment = strComment.replaceAll("<IMG src=\"images/b_edit.gif\" border=0 hspace=\"0\" alt=\"삭제하기\" align=absmiddle>", "");
        strComment = strComment.replaceAll("<IMG src=\"images/b_del.gif\" border=0 hspace=\"0\" alt=\"삭제하기\" align=absmiddle>", "");
        strComment = strComment.replaceAll("<IMG src=\"images/bt_reply.gif\" border=0 hspace=\"0\" alt=\"댓글쓰기\" align=absmiddle>", "");
        strComment = strComment.replaceAll("href=\"javascript:ui", "");
        strComment = strComment.replaceAll("href=\"JavaScript:self.location=", "");

        String strHeader = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
        strHeader += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>";
        String strBottom = "</body></html>";

        String resizeStr = "";
        
        if (strImage.length() > 0) {
            resizeStr = "<script>function resizeImage2(mm){var width = eval(mm.width);var height = eval(mm.height);if( width > 300 ){var p_height = 300 / width;var new_height = height * p_height;eval(mm.width = 300);eval(mm.height = new_height);}}</script>";
        }
    	String htmlDoc = strHeader + strTitle + resizeStr + strContent + strImage + strComment + strBottom;

        return htmlDoc;
    }

}
